package net.lijingye.education.controller;

import net.lijingye.education.entity.Token;
import net.lijingye.education.exception.AlreadyLoginException;
import net.lijingye.education.exception.DaoException;
import net.lijingye.education.exception.ObjectNotExistsException;
import net.lijingye.education.exception.PasswordNotMatchException;
import net.lijingye.education.request.ChangePasswordRequest;
import net.lijingye.education.request.LoginRequest;
import net.lijingye.education.response.BaseResponse;
import net.lijingye.education.response.ErrorResponse;
import net.lijingye.education.response.SuccessResponse;
import net.lijingye.education.response.meta.*;
import net.lijingye.education.service.AuthService;
import net.lijingye.education.vo.TokenVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by LijingYe on 2016/3/26.
 */
@RestController
public class AuthController extends BaseController {
    @Autowired
    private AuthService authService;

    @RequestMapping(value = "/auth/login", method = RequestMethod.POST)
    public BaseResponse login(@Valid @RequestBody LoginRequest loginRequest, BindingResult result) throws DaoException {
        if (result.hasErrors()) {
            return new ErrorResponse(new JSONParameterErrorMeta());
        }
        try {
            Token token = authService.doLogin(loginRequest.getId(), loginRequest.getPassword());
            TokenVO tokenVO = new TokenVO(token);
            return new SuccessResponse(tokenVO);
        } catch (PasswordNotMatchException e) {
            System.out.println(e.getClass().toString() + e.getMessage());
            return new ErrorResponse(new PasswordNotMatchMeta());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        } catch (AlreadyLoginException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new AlreadyLoginMeta());
        }
    }

    @RequestMapping(value = "/auth/logout", method = RequestMethod.POST)
    public BaseResponse logout(@RequestHeader("Authorization") String accessToken) throws DaoException {
        authService.doLogout(accessToken);
        return new SuccessResponse(null);
    }

    @RequestMapping(value = "/auth/password", method = RequestMethod.PUT)
    public BaseResponse changePassword(@RequestHeader("Authorization") String accessToken, @Valid @RequestBody ChangePasswordRequest changePasswordRequest, BindingResult result) throws DaoException {
        if (result.hasErrors()) {
            return new ErrorResponse(new JSONParameterErrorMeta());
        }
        try {
            authService.changePassword(accessToken, changePasswordRequest.getPassword(), changePasswordRequest.getNewPassword());
        } catch (PasswordNotMatchException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new PasswordNotMatchMeta());
        }
        return new SuccessResponse(null);
    }
}
